<template>
  <div class="home-page">
    <!-- Hero区域 -->
    <section class="hero-section">
      <div class="hero-content">
        <h1 class="hero-title gradient-text typewriter">探索科技前沿，洞察数字未来</h1>
        <p class="hero-subtitle">
          汇聚全球最新科技资讯，为你呈现人工智能、区块链、硬件创新的精彩世界
        </p>
        <div class="hero-stats">
          <div class="stat-item">
            <span class="stat-number">{{ appStore.formattedTotalViews }}</span>
            <span class="stat-label">总浏览量</span>
          </div>
          <div class="stat-item">
            <span class="stat-number">{{ appStore.formattedOnlineUsers }}</span>
            <span class="stat-label">在线用户</span>
          </div>
          <div class="stat-item">
            <span class="stat-number">{{ articlesStore.total }}</span>
            <span class="stat-label">精选文章</span>
          </div>
        </div>
      </div>
    </section>

    <!-- 热门文章轮播 -->
    <section class="featured-section">
      <div class="container">
        <h2 class="section-title">
          <el-icon><TrendCharts /></el-icon>
          热门精选
        </h2>
        <el-carousel
          height="400px"
          :interval="5000"
          arrow="hover"
          indicator-position="outside"
          class="featured-carousel"
        >
          <el-carousel-item
            v-for="article in articlesStore.hotArticles.slice(0, 5)"
            :key="article.id"
            class="carousel-item"
          >
            <div class="featured-card glass-effect" @click="goToArticle(article.id)">
              <div class="featured-image">
                <el-image
                  :src="article.coverImage"
                  :alt="article.title"
                  fit="cover"
                  class="carousel-image"
                />
                <div class="featured-overlay">
                  <div class="featured-content">
                    <el-tag
                      :type="getPriorityStyle(article.priority).type"
                      size="large"
                      class="featured-priority"
                    >
                      {{ getPriorityStyle(article.priority).text }}
                    </el-tag>
                    <h3 class="featured-title">{{ article.title }}</h3>
                    <p class="featured-summary">{{ truncateText(article.summary, 100) }}</p>
                    <div class="featured-meta">
                      <span>{{ article.author.name }}</span>
                      <span>•</span>
                      <span>{{ formatRelativeTime(article.publishTime) }}</span>
                      <span>•</span>
                      <span>{{ formatNumber(article.readCount) }} 阅读</span>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </el-carousel-item>
        </el-carousel>
      </div>
    </section>

    <!-- 分类导航 -->
    <section class="categories-section">
      <div class="container">
        <h2 class="section-title">
          <el-icon><Menu /></el-icon>
          热门分类
        </h2>
        <div class="categories-grid">
          <div
            v-for="category in articlesStore.categories"
            :key="category.id"
            class="category-card glass-effect glow-hover"
            @click="goToCategory(category.id)"
          >
            <div class="category-icon" :style="{ color: category.color }">
              <el-icon size="32">
                <component :is="getCategoryIcon(category.name)" />
              </el-icon>
            </div>
            <h3 class="category-name">{{ category.name }}</h3>
            <p class="category-count">{{ category.count }} 篇文章</p>
          </div>
        </div>
      </div>
    </section>

    <!-- 最新文章 -->
    <section class="latest-section">
      <div class="container">
        <div class="section-header">
          <h2 class="section-title">
            <el-icon><Clock /></el-icon>
            最新资讯
          </h2>
          <el-button
            type="primary"
            @click="loadMore"
            :loading="articlesStore.loading"
            class="load-more-btn"
          >
            {{ articlesStore.loading ? '加载中...' : '查看更多' }}
          </el-button>
        </div>

        <div class="articles-grid" v-loading="articlesStore.loading">
          <NewsCard
            v-for="article in displayedArticles"
            :key="article.id"
            :article="article"
            class="article-item"
          />
        </div>

        <!-- 无限滚动加载 -->
        <div
          v-if="articlesStore.hasMoreArticles && !articlesStore.loading"
          class="load-more-trigger"
          ref="loadMoreTrigger"
        >
          <el-button type="primary" size="large" @click="loadMore" class="load-more-button">
            加载更多文章
          </el-button>
        </div>

        <!-- 没有更多数据 -->
        <div
          v-if="!articlesStore.hasMoreArticles && displayedArticles.length > 0"
          class="no-more-data"
        >
          <el-divider>
            <el-icon><Check /></el-icon>
            已显示全部文章
          </el-divider>
        </div>
      </div>
    </section>
  </div>
</template>

<script setup lang="ts">
import { useRouter } from 'vue-router'
import NewsCard from '@/components/NewsCard.vue'
import { useArticlesStore } from '@/stores/articles'
import { useAppStore } from '@/stores/counter'

const router = useRouter()
const articlesStore = useArticlesStore()
const appStore = useAppStore()

const loadMoreTrigger = ref<HTMLElement>()
const displayedArticles = computed(() => articlesStore.latestArticles)

let intersectionObserver: IntersectionObserver | null = null

onMounted(async () => {
  // 初始化数据
  await Promise.all([articlesStore.initializeData(), appStore.initializeApp()])

  // 设置无限滚动观察器
  setupInfiniteScroll()
})

onUnmounted(() => {
  if (intersectionObserver) {
    intersectionObserver.disconnect()
  }
})

const setupInfiniteScroll = () => {
  if (!loadMoreTrigger.value) return

  intersectionObserver = new IntersectionObserver(
    (entries) => {
      entries.forEach((entry) => {
        if (entry.isIntersecting && articlesStore.hasMoreArticles && !articlesStore.loading) {
          loadMore()
        }
      })
    },
    { threshold: 0.1 },
  )

  intersectionObserver.observe(loadMoreTrigger.value)
}

const loadMore = async () => {
  await articlesStore.loadMoreArticles()
}

const goToArticle = (id: string) => {
  router.push(`/article/${id}`)
}

const goToCategory = (categoryId: string) => {
  router.push(`/category/${categoryId}`)
}
</script>

<style scoped lang="scss">
.home-page {
  min-height: 100vh;
}

// Hero区域
.hero-section {
  background: linear-gradient(135deg, var(--tech-bg-primary), var(--tech-bg-secondary));
  padding: 80px 0;
  text-align: center;
  position: relative;
  overflow: hidden;

  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: radial-gradient(circle at center, rgba(0, 212, 255, 0.1), transparent);
    pointer-events: none;
  }

  .hero-content {
    max-width: 800px;
    margin: 0 auto;
    padding: 0 20px;
    position: relative;
    z-index: 1;
  }

  .hero-title {
    font-size: 48px;
    font-weight: 800;
    line-height: 1.2;
    margin-bottom: 24px;
    letter-spacing: -1px;
  }

  .hero-subtitle {
    font-size: 18px;
    color: var(--tech-text-secondary);
    line-height: 1.6;
    margin-bottom: 40px;
  }

  .hero-stats {
    display: flex;
    justify-content: center;
    gap: 60px;

    .stat-item {
      text-align: center;

      .stat-number {
        display: block;
        font-size: 32px;
        font-weight: 700;
        color: var(--tech-accent-neon);
        margin-bottom: 8px;
      }

      .stat-label {
        font-size: 14px;
        color: var(--tech-text-muted);
      }
    }
  }
}

// 区域通用样式
section {
  padding: 60px 0;

  .section-title {
    display: flex;
    align-items: center;
    gap: 12px;
    font-size: 28px;
    font-weight: 700;
    margin-bottom: 40px;
    color: var(--tech-text-primary);

    .el-icon {
      color: var(--tech-accent-neon);
    }
  }

  .section-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 40px;
  }
}

// 热门文章轮播
.featured-section {
  background: var(--tech-bg-secondary);

  .featured-carousel {
    :deep(.el-carousel__container) {
      border-radius: 12px;
      overflow: hidden;
    }

    :deep(.el-carousel__indicator) {
      background: rgba(255, 255, 255, 0.3);

      &.is-active {
        background: var(--tech-accent-neon);
      }
    }
  }

  .featured-card {
    height: 100%;
    cursor: pointer;
    border-radius: 12px;
    overflow: hidden;

    .featured-image {
      position: relative;
      height: 100%;

      .carousel-image {
        width: 100%;
        height: 100%;
      }

      .featured-overlay {
        position: absolute;
        bottom: 0;
        left: 0;
        right: 0;
        background: linear-gradient(transparent, rgba(0, 0, 0, 0.8));
        padding: 40px;
        color: white;

        .featured-priority {
          margin-bottom: 16px;
        }

        .featured-title {
          font-size: 24px;
          font-weight: 700;
          margin-bottom: 12px;
          line-height: 1.3;
        }

        .featured-summary {
          font-size: 16px;
          line-height: 1.5;
          margin-bottom: 16px;
          opacity: 0.9;
        }

        .featured-meta {
          display: flex;
          align-items: center;
          gap: 8px;
          font-size: 14px;
          opacity: 0.8;
        }
      }
    }
  }
}

// 分类网格
.categories-section {
  .categories-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 24px;

    .category-card {
      padding: 32px 24px;
      text-align: center;
      cursor: pointer;
      border-radius: 12px;
      transition: all var(--tech-transition-normal) ease;

      .category-icon {
        margin-bottom: 16px;
      }

      .category-name {
        font-size: 18px;
        font-weight: 600;
        margin-bottom: 8px;
        color: var(--tech-text-primary);
      }

      .category-count {
        font-size: 14px;
        color: var(--tech-text-secondary);
      }
    }
  }
}

// 文章网格
.latest-section {
  .articles-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
    gap: 24px;
    margin-bottom: 40px;

    .article-item {
      animation: fadeInUp 0.5s ease forwards;
    }
  }

  .load-more-trigger {
    text-align: center;
    padding: 40px 0;
  }

  .load-more-button {
    background: var(--tech-gradient-primary);
    border: none;
    padding: 12px 32px;
    font-size: 16px;
    font-weight: 600;

    &:hover {
      transform: translateY(-2px);
      box-shadow: 0 8px 24px rgba(0, 212, 255, 0.3);
    }
  }

  .no-more-data {
    padding: 40px 0;
    text-align: center;
    color: var(--tech-text-secondary);
  }
}

// 淡入上移动画
@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

// 响应式设计
@media (max-width: 1024px) {
  .hero-section {
    padding: 60px 0;

    .hero-title {
      font-size: 36px;
    }

    .hero-stats {
      gap: 40px;

      .stat-item .stat-number {
        font-size: 28px;
      }
    }
  }

  .articles-grid {
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    gap: 20px;
  }
}

@media (max-width: 768px) {
  .hero-section {
    padding: 40px 0;

    .hero-title {
      font-size: 28px;
    }

    .hero-subtitle {
      font-size: 16px;
    }

    .hero-stats {
      flex-direction: column;
      gap: 24px;
    }
  }

  .categories-grid {
    grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
    gap: 16px;

    .category-card {
      padding: 24px 16px;
    }
  }

  .articles-grid {
    grid-template-columns: 1fr;
    gap: 16px;
  }

  .section-header {
    flex-direction: column;
    gap: 20px;
    align-items: flex-start;
  }
}
</style>
